// parse_htmls.cpp : 定义控制台应用程序的入口点。
//

#include "gumbo.h"
#include "zbj_workflow.h"
#include <stdlib.h>

int main(int argc, char** argv)
{
    int retcode;
    retcode = validate_args(argc, argv);
    if (!retcode) {
        report_msg(0, "Invalid arguments.\n");
        return 1;
    }

    const char* filename = argv[1];
    report_msg(0, "Parsing file: [");
    report_msg(0, filename);
    report_msg(0, "]\n");

    FILE* file = 0;
    file = fopen(filename, "rb");
    if (!file) {
        report_msg(0, "Load file to memory failed.\n");
        return 1;
    }

    retcode = validate_file(file);
    if (!retcode) {
        return 1;
    }

    PageTree page;
    if (!parse_filecontent(file, &page)) {
        return 1;
    }

    jsonPtr json;
    if (!output_json(&page, &json)) {
        return 1;
    }

    char* json_print_content = cJSON_Print(json);
    filename = argv[2];
    printf("\nSave to file: %s\n", filename);

    FILE* fileJson = 0;
    fileJson = fopen(filename, "wb");
    if (!fileJson) {
        report_msg(0, "Load file to memory failed.\n");
        return 1;
    }
    fwrite(json_print_content, strlen(json_print_content), 1, fileJson);
    fclose(fileJson);
    free(json_print_content);
    free(json);

    clean_pagetree(&page);
    fclose(file);
    getchar();
    return 0;
}

